﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace bLogical.BizTalkManagement.Helpers
{
    public class BackUpFile
    {
        public enum FileType { Data, Log };

        public string Server { get; set; }
        public string Database { get; set; }
        public DateTime Date { get; set; }
        public FileType Type { get; set; }
        public String MarkName { get; set; }
        public bool IsFull { get; set; }
        public bool IsLastLog { get; set; }
        public String FileName { get; set; }
        public int ProgPercent { get; set; }
        public override string ToString()
        {
            string ret = String.Format("Server: {0}, Database:{1}, Date: {2}, Type: {3}, IsFull: {4}, Mark: {5}, IsLastLog: {6}",
                this.Server,
                this.Database,
                this.Date,
                this.Type,
                this.IsFull,
                this.MarkName,
                this.IsLastLog);

            return ret;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="database"></param>
        /// <param name="fileName"></param>
        /// <param name="name"></param>
        public BackUpFile(string database, string fileName, string name)
        {
            // filename sample: SQLNAME_BAM_BAMPrimaryImport_Full_BTS_2011_11_02_12_30_11_467.bak
            // but also: SQLNAME_DEFAULT_MSHIS_HIP_Full_BTS_2011_11_02_12_30_11_467.bak
            string fileNameWithoutDatabase = fileName.Remove(fileName.IndexOf(database), database.Length + 1) ; // +1 to get trailing _
            // and now: (Lenght=11) SQLNAME_DEFAULT_Full_BTS_2011_11_02_12_30_11_467.bak

            string[] fileStrings = Path.GetFileNameWithoutExtension(fileNameWithoutDatabase).Split('_');
            this.IsLastLog = false;
            this.FileName = fileName;
            
            if (fileStrings.Length == 10) // Default instance with no instance name, only SERVER
                this.Server = fileStrings[0];
            else 
                this.Server = string.Format(@"{0}\{1}",fileStrings[0],fileStrings[1]); // SERVER\INSTANCE

            this.Database = database;

            string fullString = fileStrings.Length == 10 ? fileStrings[1] : fileStrings[2];
            this.IsFull = fullString == "Full";

            this.Type = this.IsFull ? FileType.Data: FileType.Log;

            this.Date = GetDate(fileName, name);
            this.MarkName = GetMark(fileName, name);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static DateTime GetDate(string fileName, string name)
        {
            // filename sample: SQLNAME_BAM_BAMPrimaryImport_Full_BTS_2011_11_02_12_30_11_467.bak
            // but also: SQLNAME_DEFAULT_MSHIS_HIP_Full_BTS_2011_11_02_12_30_11_467.bak
            // or: SQLNAME_DEFAULT_Full_BTS_2011_11_02_12_30_11_467.bak
            // Get the part after BTS = the date part
            string[] dateStrings = Path.GetFileNameWithoutExtension(fileName.Substring(fileName.LastIndexOf(name) + name.Length + 1)).Split('_');

            return new DateTime(int.Parse(dateStrings[0]),
                int.Parse(dateStrings[1]),
                int.Parse(dateStrings[2]),
                int.Parse(dateStrings[3]),
                int.Parse(dateStrings[4]),
                int.Parse(dateStrings[5]),
                int.Parse(dateStrings[6]));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetMark(string fileName, string name)
        {
            // filename sample: SQLNAME_BAM_BAMPrimaryImport_Full_BTS_2011_11_02_12_30_11_467.bak
            // but also: SQLNAME_DEFAULT_MSHIS_HIP_Full_BTS_2011_11_02_12_30_11_467.bak
            // or: SQLNAME_DEFAULT_Full_BTS_2011_11_02_12_30_11_467.bak
            // Get the part after BTS = the date part
            string[] dateStrings = Path.GetFileNameWithoutExtension(fileName.Substring(fileName.LastIndexOf(name) + name.Length + 1)).Split('_');

            return String.Format("{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}",
                name,
                dateStrings[0],
                dateStrings[1],
                dateStrings[2],
                dateStrings[3],
                dateStrings[4],
                dateStrings[5],
                dateStrings[6]);
        }
    }
}
